﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using DoNet.FrameWork.Services.Impl;
using DoNet.FrameWork.Repository;
using DoNet.FrameWork.Repository.Specification;
using Authority.Entity;
using Authority.Repositories;

namespace Authority.Services.Impl
{
    public class PermisionServiceImpl:IPermisionService
    {
        private readonly IUnitOfWork _context;
        private readonly IPostControlsMapRepository _postControlsMapRepository;
        private readonly IUserControlsMapRepository _userControlsMapRepository;

        public PermisionServiceImpl(IUnitOfWork context, IPostControlsMapRepository postControlsMapRepository, IUserControlsMapRepository userControlsMapRepository)
        {
            _context = context;
            _postControlsMapRepository = postControlsMapRepository;
            _userControlsMapRepository = userControlsMapRepository;
        }

        public string UpdatePostPermision(Guid postId, List<PostControlsMap> pcms)
        {
            //先删除再插入
            ISpecification<PostControlsMap> condition = new DirectSpecification<PostControlsMap>(p => p.PostId == postId);
            _postControlsMapRepository.Removes(condition);
            foreach (var pcm in pcms)
            {
                _postControlsMapRepository.NewSave(pcm);
            }
            _context.Commit();
            return "保存成功!";
        }

        public string UpdateUserPermision(Guid userId, List<UserControlsMap> ucms)
        {
            //先删除再插入
            ISpecification<UserControlsMap> condition = new DirectSpecification<UserControlsMap>(u => u.UserId == userId);
            _userControlsMapRepository.Removes(condition);
            foreach (var ucm in ucms)
            {
                _userControlsMapRepository.NewSave(ucm);
            }
            _context.Commit();
            return "保存成功!";
        }

        public List<PostControlsMap> GetPostPermision(Guid postId)
        {
            ISpecification<PostControlsMap> baseCondition = new DirectSpecification<PostControlsMap>(p => p.IsDeleted == false);
            ISpecification<PostControlsMap> postCondition = new DirectSpecification<PostControlsMap>(p => p.PostId == postId);
            postCondition = new AndSpecification<PostControlsMap>(baseCondition, postCondition);
            Expression<Func<PostControlsMap, string>> orderbyExpression = p => p.ModulesCode;
            return _postControlsMapRepository.GetList<string>(postCondition, orderbyExpression, false);
        }

        public List<UserControlsMap> GetUserPermision(Guid userId)
        {
            ISpecification<UserControlsMap> baseCondition = new DirectSpecification<UserControlsMap>(u => u.IsDeleted == false);
            ISpecification<UserControlsMap> userCondition = new DirectSpecification<UserControlsMap>(u => u.UserId == userId);
            userCondition = new AndSpecification<UserControlsMap>(baseCondition, userCondition);
            Expression<Func<UserControlsMap, string>> orderByExpression = u => u.ModulesCode;
            return _userControlsMapRepository.GetList<string>(userCondition, orderByExpression, false);
        }

    }
}
